package org.finesys.boot.controller;

import javax.servlet.http.HttpServletRequest;

import org.finesys.common.core.module.R;
import org.finesys.common.core.util.WebUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.anji.captcha.model.common.CaptchaTypeEnum;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;

import lombok.RequiredArgsConstructor;


@RestController
@RequestMapping
@RequiredArgsConstructor
public class ValidCodeController {
    private final CaptchaService captchaService;
    @GetMapping("/code")
    public R<ResponseModel> getCode(HttpServletRequest request){
        CaptchaVO vo = new CaptchaVO();
        vo.setCaptchaType(CaptchaTypeEnum.BLOCKPUZZLE.getCodeValue());
        vo.setBrowserInfo(WebUtils.getClientIP(request));
        ResponseModel responseModel = captchaService.get(vo);
        return R.success(responseModel);
    }

    @PostMapping("/code/check")
    public R<ResponseModel> codeCheck(@RequestBody CaptchaVO captchaVO,HttpServletRequest request){
        captchaVO.setBrowserInfo(WebUtils.getClientIP(request));
        if(!StringUtils.hasText(captchaVO.getCaptchaType())){
            captchaVO.setCaptchaType(CaptchaTypeEnum.BLOCKPUZZLE.getCodeValue());
        }
        if (!StringUtils.hasText(captchaVO.getPointJson())) {
            captchaVO.setPointJson(request.getParameter("pointJson"));
        }
        if (!StringUtils.hasText(captchaVO.getToken())) {
            captchaVO.setToken(request.getParameter("token"));
        }
        ResponseModel responseModel = captchaService.check(captchaVO);
        return R.success(responseModel);
    }
}
